import music
from common import PlayEntity, SearchFilter, SongEntity
from common import ArtistEntity, AlbumEntity
from businessrules import AlbumBiz, ArtistBiz
from dataaccess import ArtistData, AlbumData

def createNewSong(songName, artistName, albumName, description):
    """inserts a new song and records a play for that song"""

    sf = SearchFilter.SimpleFilter("AlbumName",
                    SearchFilter.ConditionType.Equal,albumName)
    albumID = AlbumData.AlbumData().SelectAlbumByFilter(sf)

    #see if the artist exists
    sf = SearchFilter.SimpleFilter("Name",
                    SearchFilter.ConditionType.Equal,artistName)
    artistID = ArtistData.ArtistData().SelectArtistByFilter(sf)

    #if there exists no artist, insert a new one
    if not artistID:
        artistEntity = ArtistEntity.ArtistEntity(artistID=-1,name=artistName)
        aritstID = ArtistBiz.ArtistBiz().InsertArtist(artistEntity)

    #if there exists no album, insert a new 
    if not albumID:
        album = AlbumEntity.AlbumEntity(albumID=-1,
                    artistID=artistID, 
                    albumName=albumName,
                    description=description,
                    artistName=artistName)
        albumID = AlbumBiz.AlbumBiz().InsertAlbum(album)

    #create the song
    song = SongEntity.SongEntity(
                    visibility=1,
                    name=songName,
                    songID=-1,
                    albumID=albumID)
    songID = SongData.SongData.InsertSong(song)
    return songID
